home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ShareWare OnLine 2
/
ShareWare OnLine Volume 2 (CMS Software)(1993).iso
/
games2
/
rotise12.zip
/
MISC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-04-03
|
7KB
|
313 lines
/* misc.c -- Miscellaneous routines
December 1989 Mark E. Mallett
This file contains misc routines for the WRAP program. Some of these
were lifted from FFS.
Included are:
asknum Prompt and ask for a number
atonum Convert ASCII to LONG number
emalloc malloc() with error message
erealloc realloc() with error message.
error Handle error message
getline Input a line of text from a file.
gettkline Input a line of text && tokenize it
ndtmtotv Convert date/time digit string to tm struct
nextonum Convert ASCII hex to LONG number
prtdat Print date
prtdtm Print date and time
prttim Print time
prtndat Print date as a number string
prtndtm Print date and time as a number string
prtntim Print time as a number string
vtell Print according to verbosity level
warning Handle warning message.
*/
#include <stdio.h>
#ifdef __TURBOC__
#include <stdlib.h>
#include <alloc.h>
#else
#include <malloc.h>
#endif
#include <ctype.h>
#include <time.h>
#include "rotise.h"
#include "bb.h"
/* Local Definitions */
/* External data referenced */
extern int Vlevel; /* Current verbosity level */
/* External routines used */
extern char *gettoken();
extern LONG os_tmclock();
/* Local data publicly available */
/* Local routines and forward references */
void error PROTO( (int code, char *fmtP, int a1, int a2, int a3) );
long atonum();
long hextonum();
/* Private data */
static char Errbuf[200]; /* Buffer for formatted error text */
/*
*//* emalloc( facP, nameP, size )
Allocate memory, give error if failure.
Accepts :
facP Name of facility (e.g., subroutine name)
nameP Name of item being allocated
size Desired size
Returns :
< value > Ptr to thing.
Notes :
error return is taken if allocation fails.
*/
void *
emalloc( facP, nameP, size )
char *facP; /* Ptr to facility name */
char *nameP; /* Ptr to name of thing */
int size; /* Number of bytes */
{
char *newP; /* Ptr to memory */
if ( ( newP = malloc( size ) ) == NULL )
error( EC_MEMORY, "%s: Error allocating %d bytes for %s",
(int)facP, size, (int)nameP );
return( (void *)newP );
}
/*
*//* erealloc( facP, nameP, oldP, size )
Reallocate memory, give error if failure.
Accepts :
facP Name of facility (e.g., subroutine name)
nameP Name of item being allocated
oldP Ptr to old thing (may be NULL)
size Desired size
Returns :
< value > Ptr to thing.
Notes :
error return is taken if allocation fails.
*/
void *
erealloc( facP, nameP, oldP, size )
char *facP; /* Ptr to facility name */
char *nameP; /* Ptr to name of thing */
char *oldP; /* Ptr to existing thing */
int size; /* Number of bytes */
{
char *newP; /* Ptr to memory */
if ( oldP == NULL )
newP = malloc( size );
else
newP = realloc( oldP, size );
if ( newP == NULL )
error( EC_MEMORY, "%s: Error re-allocating %d bytes for %s",
(int)facP, size, (int)nameP );
return( (void *)newP );
}
/*
*//* gettkline( fP, bufP, bufsize, tokcP, tokv, tokmax )
Input a line of text and tokenize it.
Accepts :
fP Stream for input file
bufP Buffer for line
bufsize Size of the buffer
tokcP Ptr to tokc variable -- # of tokens
tokv Ptr to tokv array -- ptrs to tokens
tokmax Maximum number of tokens to return
Returns :
<value> Number of characters read (-1 if EOF)
*tokcP Number of tokens received
*tokv Ptrs to tokens
Notes :
Excess input (up to a newline) is ignored.
The line buffer is used for the token buffer as well.
*/
int
gettkline( fP, bufP, bufsize, tokcP, tokv, tokmax )
FILE *fP; /* Input file ptr */
char *bufP; /* Buffer ptr */
int bufsize; /* Room in buffer */
int *tokcP; /* Variable to hold token count */
char **tokv; /* Arg vectors */
int tokmax; /* Max # tokens */
{
int cC; /* Characters */
/* Input the line */
cC = getline( fP, ++bufP, bufsize-1 );
/* Use tkline() to tokenize it */
*tokcP = tkline( bufP, bufP-1, tokv, tokmax, "", " \t,:" );
return( cC );
}
/*
*//* getline( fP, bufP, bufsize )
Inputs a line of text from a file.
Accepts :
fP Stream for input file
bufP Buffer for line
bufsize Size of the buffer
Returns :
<value> Number of characters read (-1 if EOF)
Notes :
Excess input (up to a newline) is ignored.
*/
getline( fP, bufP, bufsize )
AREG1 FILE *fP; /* Input file ptr */
AREG2 char *bufP; /* Buffer ptr */
int bufsize; /* Room in buffer */
{
DREG1 int cC; /* Characters */
DREG2 int ch; /* Character */
for( --bufsize, cC = 0; ( ( ch = getc( fP ) ) != '\n' ) && ( ch != EOF ); )
if ( cC++ < bufsize )
*bufP++ = ch;
*bufP = NUL;
if ( ( cC == 0 ) && ( ch == EOF ) )
cC = -1;
return( cC );
}
/*
*//* error( code, fmtP, args... )
One-up interface to return_error
Accepts :
code Status code
fmtP printf-style format string
args printf arguments. Quantity is limited.
Returns :
Nothing. Formats the string and calls return_error.
Notes :
Handling of the arguments is kludgy.
*/
#ifdef __TURBOC__
void error ( int code, char *fmtP, int a1, int a2, int a3 )
#else
void
error( code, fmtP, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 )
int code; /* Status code */
char *fmtP; /* message format string */
int a1, a2, a3, /* Like I said... */
a4, a5, a6, /* ... it's a kludge. */
a7, a8, a9, a10;
#endif
{
sprintf( &Errbuf[0], fmtP, a1, a2, a3
#ifndef __TURBOC__
, a4, a5, a6, a7, a8, a9, a10
#endif
);
fprintf( stderr, "%d: %s.\n", code, Errbuf );
exit( 1 );
}
/*
*//* warning( code, fmtP, args... )
Issue a warning (a la error(), but using note_error)
Accepts :
code Status code
fmtP printf-style format string
args printf arguments. Quantity is limited.
Returns :
Nothing; calls note_error and returns.
Notes :
Handling of the arguments is kludgy.
*/
void
warning( code, fmtP, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 )
int code; /* Status code */
char *fmtP; /* message format string */
int a1, a2, a3, /* Like I said... */
a4, a5, a6, /* ... it's a kludge. */
a7, a8, a9, a10;
{
sprintf( &Errbuf[0], fmtP, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 );
fprintf( stderr, "%d: %s.\n", code, Errbuf );
}